package com.alianzamedica.controllers.reports;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.objectsearch.web.tools.Converter;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.businessobject.SalesByMedicObject;
import com.alianzamedica.models.ReportModel;
import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class SalesByMedicReportAction extends Action {

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Integer medicId = Converter.string2Integer(request.getParameter("id"));

		ArrayList medicList = getMedicList();
		request.setAttribute("medics", medicList);
		if (medicId != null) {
			ReportModel model = new ReportModel();
			ArrayList<SalesByMedicObject> list = model
					.getMedicSalesReport(medicId);
			request.setAttribute("drugsales", list);
			request.setAttribute("medicId", medicId);
		}

		return mapping.findForward("success");
	}

	@SuppressWarnings("unchecked")
	private ArrayList getMedicList() throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		Doctor d = new Doctor();
		ArrayList list = search.searchObjects(d);
		return list;
	}

}
